Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Showing posts with label C Aptitude Questions And Answers. Show all posts
Showing posts with label C Aptitude Questions And Answers. Show all posts
10:04 am

C Aptitude Questions And Answers - ARRAY

---------------------------------- Test Your "C" Skill -------------------------------------

ARRAY  

1.void main()
{
 char arr[]=”Visual C++”
 char *arr= “Visual C++”
 printf(“\n %d %d”, sizeof(a), sizeof(b));
 printf(“\n %d %d”, sizeof(*a), sizeof(*b));

}
Output:- 11 2
                 1  1

2.   For the following statements would arr[3] and ptr[3] fetch the same character?
char arr[]=”ItChallengers”
char *ptr=”ItChallengers

Ans:YES.

3. void main()
    {
        int a[3] = {10, 20, 30};
        int *p = a;
        printf("%p\t%p", p, a);
    }


a) Same address is printed.

b) Different address is printed.

c) Compile time error
d) Nothing

Output:- A

4.What does the following declaration mean?
int (*ptr)[10];
A. ptr is array of pointers to 10 integers
B. ptr is a pointer to an array of 10 integers
C. ptr is an array of 10 integers
D. ptr is an pointer to array

Output:- B

5.
int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}

A. 2, 1, 15
B. 1, 2, 5
C. 3, 2, 15
D. 2, 3, 20

Output:- C

6.
void main()
{
   int a[3][4] = {
                         1, 2, 3, 4,
                         4, 3, 2, 8,
                 7, 8, 9, 0
                         };
   int *ptr;
   ptr = &a[0][0];
   fun (&ptr);
}

fun (int **p)
{
  printf(“\n%d”,**p);
}

Output:-   1

7.What would be the o/p of the following program if the array begins at 65442?

void main()
{
 int a[3][4] = {
                         1, 2, 3, 4,
                         4, 3, 2, 1,
                         7, 8, 9, 0
                    };
 printf(“\n %u  %u”, a+1, %a+1);
}

Output:-    65450  65466

8.
void main()
{
  float arr[]= { 12.4, 2.3, 4.5, 6.7};
  printf(“\n %d”, sizeof(arr)/sizeof(arr[0]));
}


Output:-   4

9.   Would the following program compile successfully?
void main()
{
 char a[]= “Hello”;
 char *p= “ItChallengers”;
 a=”ItChallengers”;
 p=”Hello”;
 printf(“\n %s  %s”, a, p);
}

Output:-  NO. Because we may assign a new string to a pointer but not to an array.

10.     What be the output of the following program if the array begins at 65486?
void main()
{
 int arr[]= {12, 14, 15, 23, 45}
 printf(“%u  %u”, arr+1, &arr+1);
} 

      Output: 65488  65496


11.  What would be the o/p of the following program, if the array begins at address 1200?

void main()
{
 int  arr[]={2, 3, 4, 1, 6};
 printf(“%d  %d”, arr, sizeof(arr));
} 

    Output:  1200  10


12.   What would be the O/p of the following program, if the array begind at address 65486?

void main()
{
 int arr[]={12, 14, 15, 23, 45};
 printf(“%u  %u”,arr, &arr);
} 

        Output: 65486  65486


12:02 pm

C Aptitude Questions And Answers - C PREPROCESSOR

---------------------------------- Test Your "C" Skill -------------------------------------

PRE PROCESSOR  


1. Identify the token pasting operator?
a) +
b) ++
c) #
d) ##

Output:- D


2.

#include<stdio.h>
#define MAX 15


int main(){

    int num;
    num = ++MAX;
    printf("%d",num);
    return 0;
}
a.15
b.16
c.compile error
d.run time error

Output:- compile error


3.

#define MAX (x, y) ((x) > (y) ? (x) : (y))
main()
{
int x=5, y = 5;
printf(“maximum is %d”, MAX( ++x, ++y));
}


a) Maximum is 7

b) Maximum is 5
c) Maximum is 6
d) None 


Output:- A



4.

#define square (a) (a * a)
printf(“%d”, square (4 + 5));


a) 81

b) 4
c) 29
d) None 

Output:- C


5.

#define SQR(x) (x * x)
 main()
 {
  int a,b=3;
  a=SQR(b+2);
  printf(“\n%d”,a);
 }
a. 25
b. 11
c. error
d. garbage value

Output:- B


6.

#define SQR(x)( (x) *( x))
 main()
 {
  int a,b=3;
  a=SQR(b+2);
  printf(“\n%d”,a);
 }
a. 25
b. 11
c. error
d. garbage value

Output:- a


7.

#define CUBE(x) (x * x * x)
 main()
{
 int a,b=3;
 a=CUBE(b++);
 printf(“\n %d %d”,a,b);
 }

Output:- 27  6


8.

#define SWAP(a,b,c) (int t; t=a, a=b, b=t;)
main()
{
 int x=10,y=20;
 SWAP(x,y,int);
 Printf(“%d %d”,x,y);
}

Output:- (int t; t=a, a=b, b=t;);

 This code won’t compile since the declaration of t can’t take place within parenthesis.


9.How should you modify SWAP macro such that it can swap two integers?




Output:- #define SWAP(a,b,c)  c t; t=a, a=b, b=t;


10.

#define MESS junk

void main(){   printf(“MESS”);}

Output:- MESS

11.

#define MAX(a,b) (a>b?a:b)

main()
{
   int x;
   x=MAX(3+2,2+7);
   printf(“%d”,x);
}

Output:-  9

12.
#define PRINT(int) printf(“%d”,int)
main()
{
 int x=2,y=3,z=4;
 PRINT(x);
 PRINT(y);
 PRINT(z);
}

Output: 2 3 4

9:59 am

C Aptitude Questions And Answers - POINTER

---------------------------------- Test Your "C" Skill -------------------------------------

  POINTERS


1.Can you combine the following two statements into one

char *p;
p=malloc(100);

Output: char *p=malloc(100);

2.Can you split the following statement into two statements?

Char far *str=(char far*) 0xb8000000L;

Output:
char far *str;
str=(char far*) 0xb8000000L;

3.Are the expressions *ptr++ and ++*ptr same?

Output: No.
*ptr++ increments the pointer and not the value pointed by it, 
whereas ++*ptr increments the value being pointed to by ptr.

4. Can You write another expression which does the same job as ++*ptr?

Output:  (*ptr)++

5.What would be the equivalent pointer expression for referring the same element as a[i][j][k][l]?

Output:   *(*(*(*(a+i)+j)+k)+l)

6.
void main()
{
  int arr[]=(12,13,14,15,16};
  printf(“\n%d %d %d”,sizeof(arr),sizeof(*arr),sizeof(arr[0]));
}

Output: 10 2 2

7.What would be the O/P of the program assuming that the array begins at 1002?
void main()
{
  int a[3][4]={
                       1,2,3,4,
                       5,6,7,8,
                       9,10,11,12
                   };
     printf(“\n%u %u %u”,a[0]+1,*(a[0]+1),*(*(a+0)+1));
}

Output: 1004 2 2

8.What would be the output of the program assuming that the array begins at location 1002?
void main()
{
  int a[2][3][4]={
                           {
                               1,2,3,4,
                               5,6,7,8,
                               9,1,1,2
                           },
                          {
                              2,1,4,7,
                              6,7,8,9,
                              0,0,0,0
                          }
                      }
 printf(“\n %u  %u  %u  %d”,a,*a,**a,***a);

 Output:1002  1002  1002  1

9.In the following program how would you print 50 using p?
void main()
{
  int a[]={10,20,30,40,50}
  char *p;
  p=(char*)a;
}

Output: printf(“\n %d”,*((int*)p+4));

10.In the following program add a statement in the function fun() such that the address of  a  gets stored in j.
void main()
{
  int *j;
  void fun(int**);
  fun(&j);
}
void fun(int**k)
{
  int a=10;
 /* add statement here*/
}

Output:  *k=&a;

11.Where can one think of using pointers?

Output
--Accessing array or string elements
--Dynamic memory allocation
-- Call by reference
-- Implementing linked lists, trees, graphs, and many other data structures
 Etc.

12.How would you implement an array of three function pointers where each function receives two int and return a float?

Output: float (*arr[3])(int,int);

14. Would the following program give a compilation error or warning?
void main()
{
  float i=10,*j;
  void *k;
  k=&i;
  j=k;
  printf(“\n%f”,*j);
}

Output: No. 
Here no type casting is required while assigning the value to and from k because conversions are applied automatically when other pointer types are assigned to and from void*.

15. Would the following program compile?
void main()
{
  int a=10,*j;
  void *k;
  j=k=&a;
  j++;
  k++;
  printf(“\n %u  %u”,j,k);
}

Output:-void pointers is not permitted unless the void pointer is appropriately type casted.
  
16. Would the following code compile successfully?
void main()
{
  printf(“%c”,7[“ItChallengers”]);
}


Output: YES.
 Prints e of ItChallengers.
3:23 am

C Aptitude Questions And Answers -FUNCTIONS

---------------------------------- Test Your "C" Skill --------------------------------------

FUNCTIONS

1.
void main()
{
  int a,b;
  a=sumdig(123);
  b=sumdig(123);
  printf(“%d %d”,a,b);
}

sumdig(int n)
{
  static int s=0;
  int d;
  if(n!=0)
  {
  d=n%10;
  n=(n-d)/10;
  s=s+d;
  sumdig();
  }
 else
    return(s);
}

Output: 6 12

2.What error would the following function give on compilation.

F(int a,int b)
{
 int a;
 a=20;
return a;
} 
a .missing parenthesis in return statement.
B .The function should be defined as   int f(int a,int b)
C . Redeclaratin of a.
d.     None of above.

Output: C.

3.Thee is a mistake in the following code. Add a statement in it to remove it.
void main()
{
 int a;
 a=f(10,3.14);
 printf(“%d”,a);
}

f(int aa,float bb)
{
 return((float(aa)+bb);
}

Output: Add the following function prototype in main()
  float f(int aa,float bb);

4. Point error in the following code.
void main()
{
 int a=10;
 void f();
 a=f();
 printf(“%d”,a);
}

void f()
{
 printf(“HI”);
}

Output:- In spite of decelerating that the function will return void the program is trying to collect the value in a variable.

5.Point error if any
void main()
{
 int b;
 b=f(20);
 printf(“%d”,b);
 }

int f(int a)
{
  a>20?return(10):return(20);
}

Output:- Return statement can not be used in format as shown in the conditional operator instead it should be as follows
  return(a>20?10:20);

6. A function can not be defined inside another function. 
a. true
b. false

Output: a.

7.Will the following function work?<yes/no>

f1(int a, int b)
{
   return(f2(20));
}

f2(int a)
{
 return(a*a);
}

Output:-YES.

8.What are following two notations of defining functions commonly known as

 int f(int a, float b)
{
  /* some code */
}

int f(a,b)
int a, float b;
{
  /* some code */
}

Output:- The first one is known as ANSI notation. 
And the second one is known as Kernighan and Ritche.
or simply K & R notation

9.In function two return statements should not occur successively.
 a. True
 b.  False

Output:- a.

10.In C all functions except main() can be called recursively.

Output:- FALSE. 
Any function including main() can be called recursively.

11. Usually recursion works slower than loops.
 a. True
 b. False

Output:- a.

12.Is it true that too many recursive calls may result in stack overflow?

Output:- TRUE.

13. How many times the following program prints ‘ITChallengers’?
void main()
{
 printf(“\n ITChallengers”);
 main();
}
a.      infinite.
b.     32767 times
c.      65535 times
d.     Till stack doesn’t overflow.


Output:- D